﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using AppCommon;
using Controller;

public partial class Tintuc_detail : System.Web.UI.Page
{
    public static string urlWebsite = ConfigurationManager.ConnectionStrings["URLWebsite"].ToString();
    public static string linkTintuc;
    public static string _meta_description;
    
    public static string _meta_title;
    public static string _meta_image;
    public static string _meta_url;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var uniqueId = Request.QueryString["uniqueid"];
            if (uniqueId != null)
            {
                BindData(uniqueId);
            }
            BindQuangCao07(Constants.QC7);
            BindQuangCao08(Constants.QC8);
        }
    }

    public void BindData(string uniqueId)
    {
        try
        {
            var controller = new VYC06Controller();
            var model = controller.GetByUniqueId(uniqueId, 1);

            if (model.Count > 0)
            {
                litTieude.Text = model[0].TieuDe;
                litTitlePage.Text = model[0].TieuDe;
                _meta_description = model[0].TomTat;
                _meta_title = model[0].TieuDe;
                litNgaycapnhat.Text = model[0].NgayCapNhat.ToString("dd/MM/yyyy");
                litTacgia.Text = model[0].TacGia;
                linkTintuc = urlWebsite + "tintuc/" + model[0].UniqueId;
                litNoidung.Text = Server.HtmlDecode(model[0].NoiDung);
                _meta_image = urlWebsite + "Upload/Tintuc/" + model[0].AnhDaiDien;
                _meta_url = urlWebsite + "tintuc/" + model[0].UniqueId;
                litView.Text = model[0].LuotXem.ToString();
                repeaterTags.DataSource = SpilitTags(model[0].Tag);
                repeaterTags.DataBind();
                // update count view
                var controller1 = new VYC06Controller();
                controller1.UpdateView(model[0].Id, model[0].LuotXem + 1);
            }
            else
            {
                Response.Redirect("~/404.aspx");
            }
        }
        catch (Exception)
        {
            Response.Redirect("~/404.aspx");
            throw;
        }
    }

    public List<String> SpilitTags(string tag)
    {
        var tags = tag.Split(',');
        return tags.Select(s => s.Replace(' ', '-')).ToList();
    }

    public void BindQuangCao07(string uniqueid)
    {
        var controller = new VYC05Controller();
        var model = controller.GetByUniqueId(uniqueid);

        // Get quang cao 1
        if (model.Count > 0)
        {
            var controller1 = new VYC06Controller();
            var model1 = controller1.GetBySubLoaiCm(model[0].Id, 1);

            // Set link and image
            if (model1.Count > 0)
            {
                linkQuangcao02.NavigateUrl = model1[0].LienKet;
                imgQuangcao02.ImageUrl = Constants.UPLOAD_TINTUC + model1[0].AnhDaiDien;
                imgQuangcao02.AlternateText = model1[0].TieuDe;
                imgQuangcao02.ToolTip = model1[0].TieuDe;
            }

        }
    }
    public void BindQuangCao08(string uniqueid)
    {
        var controller = new VYC05Controller();
        var model = controller.GetByUniqueId(uniqueid);

        // Get quang cao 1
        if (model.Count > 0)
        {
            var controller1 = new VYC06Controller();
            var model1 = controller1.GetBySubLoaiCm(model[0].Id, 1);

            // Set link and image
            if (model1.Count > 0)
            {
                linkQuangcao08.NavigateUrl = model1[0].LienKet;
                imgQuangcao08.ImageUrl = Constants.UPLOAD_TINTUC + model1[0].AnhDaiDien;
                imgQuangcao08.AlternateText = model1[0].TieuDe;
                imgQuangcao08.ToolTip = model1[0].TieuDe;
            }

        }
    }
}